export function parseURL(url) {
  const reg = /^([a-zA-z]+):\/\/((?:[\w-]+\.)+[\w]+)(\/?.*)(\?.+)?(#.+)?$/gi
  if (reg.test(url)) {
    const a = document.createElement('a')
    a.href = url
    return {
      href: url,
      protocol: a.protocol,
      host: a.hostname,
      port: a.port,
      search: a.search,
      hash: a.hash,
      path: a.pathname,
    }
  } else {
    throw new Error('invalid url')
  }
}

export function addTokenToUrl(url, putTokenKey, getTokenKey, href) {
  const token = getUrlSearch(href || window.location.href, getTokenKey)
  let urlObj
  try {
    urlObj = parseURL(url)
  } catch (e) {
    return e.message
  }
  const hash = urlObj.hash
  const protocol = urlObj.protocol
  const host = urlObj.host
  const port = urlObj.port
  let search = urlObj.search
  const path = urlObj.path
  if (search) {
    search = `${search}&${putTokenKey}=${token}`
  } else {
    search = `?${putTokenKey}=${token}`
  }
  return `${protocol}//${host}${port ? `:${port}` : ''}${path}${search}${hash}`
}

export function getUrlSearch(url, key) {
  let urlObj
  try {
    urlObj = parseURL(url)
  } catch (e) {
    return e.message
  }
  const query = urlObj.search.substring(1)
  const attr = {}
  let match
  const pl = /\+/g
  const search = /([^&=]+)=?([^&]*)/g
  const decode = (s) => {
    return decodeURIComponent(s.replace(pl, ' '))
  }
  while ((match = search.exec(query))) {
    attr[decode(match[1])] = decode(match[2])
  }

  if (key) {
    return attr[key] || ''
  } else {
    return attr
  }
}

export function getDateCategory(date) {
  const today = new Date()
  today.setHours(0, 0, 0, 0)
  const yesterday = new Date(today)
  yesterday.setDate(yesterday.getDate() - 1)
  const oneWeekAgo = new Date(today)
  oneWeekAgo.setDate(oneWeekAgo.getDate() - 7)
  const oneMonthAgo = new Date(today)
  oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1)

  if (date > today) {
    return '今天'
  } else if (date > yesterday) {
    return '昨天'
  } else if (date > oneWeekAgo) {
    return '近一周'
  } else if (date > oneMonthAgo) {
    return '近一个月'
  } else {
    return (
      date.toLocaleString('default', { month: 'long' }) +
      `${
        date.getFullYear() === today.getFullYear()
          ? ''
          : ` ${date.getFullYear()}`
      }`
    )
  }
}
import dayjs from 'dayjs'
export function getDayjsCategory(createTime) {
  // 创建一个dayjs日期对象
  const date = dayjs(createTime, 'YYYY-MM-DD HH:mm:ss')
  // 创建各种比较时间点
  const today = dayjs().startOf('day') // 今天零点
  const yesterday = dayjs().subtract(1, 'day').startOf('day') // 昨天零点
  const oneWeekAgo = dayjs().subtract(1, 'week').startOf('day') // 一周前零点
  const oneMonthAgo = dayjs().subtract(1, 'month').startOf('day') // 一月前零点
  if (date.isAfter(today)) {
    return '今天'
  } else if (date.isAfter(yesterday)) {
    return '昨天'
  } else if (date.isAfter(oneWeekAgo)) {
    return '近一周'
  } else if (date.isAfter(oneMonthAgo)) {
    return '近一个月'
  } else {
    const monthName = date.format('MMMM') // 获取月份全名
    const year = date.year()
    const currentYear = dayjs().year()
    return monthName + (year === currentYear ? '' : ` ${year}`)
  }
}

export function decodeHTML(str) {
  const elem = document.createElement('textarea')
  elem.innerHTML = str
  return elem.textContent
}

export function removeImageTags(htmlString, str) {
  // 使用正则表达式匹配<img>标签并替换为空字符串
  return htmlString.replace(/<img[^>]*>/g, str || '')
}
